package com.lianniu.mgr.util;

import java.lang.reflect.Field;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.supplyManage.vo.SupplyVo;

public class ObjectUtil {
	/**
	 * 将对象中空属性设置成空字符串
	 * 注意：目前仅设置字符串类型的值
	 * 注意：对继承的属性无效
	 * @param object
	 */
	public static void nvlProperty(Object object) {
		if(object == null){
			return ;
		}
		Class<?> cl = object.getClass();
		Field[] fields = cl.getDeclaredFields();
		if (fields == null || fields.length == 0) {
			return;
		}
		for (Field field : fields) {
			try {
				field.setAccessible(true);
				Object value = field.get(object);
				Class<?> fieldType = field.getType();
				if (value == null && fieldType.isAssignableFrom(String.class)) {
					field.set(object, "");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String...strings){
		nvlProperty(null);
		SupplyVo su = new SupplyVo();
		nvlProperty(su);
		System.out.println(JSON.toJSONString(su));
	}
}
